package com.wn.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Field;

public class MyBeanUpperCase implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization()方法被调用了。。。。");
        //根据对象获取该对象中定义的字段（属性）
        Field[] fields = bean.getClass().getDeclaredFields();
        for (Field field : fields){
            //判断属性类型

            // 针对String类型的Field成员加以修正
            if(field.getType().equals(String.class)) {
                //设置可访问
                field.setAccessible(true);
                try {
                    //获取原有的值
                    String original =(String)field.get(bean);
                    // 改为大写
                    field.set(bean, original.toUpperCase());
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization()方法被调用了。。。。");
        return bean;
    }
}
